<?php 

	/*****************taobao数据转换函数放入********涵数名入数据表open_transport中act字段*********************/
	//	12,"类目2分类",NULL,NULL,"itemcat2cat"
	//	13,"宝贝2商品",NULL,NULL,"item2goods"
	//	14,"品牌2品牌",NULL,NULL,"brand2brand"
	//	15,"属性2颜色",NULL,NULL,"prop2color"
	//	16,"属性2尺码",NULL,NULL,"prop2size"
	//	17,"属性2系列",NULL,NULL,"prop2series"
	//	18,"属性2季节",NULL,NULL,"prop2season"
	//  19,"交易2订单",NULL,NULL,"trade2order"
	//  20,"买家2会员",NULL,NULL,"buyer2user"

	/**
	 *  延缓同步TOP发货功能  OS异常发货。后期可以同步TOP 
	 * */
	function delay_TOP_SendOrder(){
		global  $db,$os,$topParamArr;
		$sql="select order_sn from open_order_info as oi 
			  left join  open_tb_trade as tb_t on oi.order_sn= tb_t.outer_order_sn 
			  where oi.shipping_status=1 and oi.invoice_no<>'' and tb_t.STATUS='WAIT_SELLER_SEND_GOODS'";
		 if($rows = $db->getAll($sql)){ 
			 foreach ( $rows as $row )  if($order_sn = $row['order_sn']) taobaoSendOrder($order_sn); 
		 } 
	}
	/**
	 * OS中的inventory库存同步TOP中的SKU
	 * */
	function inventory2SKU(){
		global  $db,$os,$topParamArr;
		$sql="select tb_item.num_iid, tb_item.cid,tb_ic.name as icname ,tb_item.title,tb_item.props,tb_item.property_alias, 
				tb_item.seller_cids,tb_item.iid ,g.goods_name,g.goods_sn 
				from open_tb_item as tb_item 
				left join open_tb_itemcat as tb_ic on tb_item.cid =tb_ic.cid 
				left join open_goods as g on tb_item.outer_id =g.goods_sn 
				where goods_sn <>'' and nick='".API_USER."' ORDER BY tb_item.id DESC"; 
		 if($rows = $db->getAll($sql)){ 
				 foreach ( $rows as $row )  { //cid  goods_sn
						if( $iid = $row['iid'])  UpLoadSKU( $row['goods_sn'], $iid ,$row['cid'], 1 ); //TOP_Item_Update_SKU($iid);  
				 } 
		 }
	}
	 /**
	 *  类目2分类
	 * */
	function itemcat2cat(){
	    global  $db,$os,$topParamArr;
		$sql="insert into open_category(cat_name,parent_id,show_in_nav,style,grade,filter_attr,goods_type,cid) 
			  select   name,0,1,'',0,0,0,tb_itemcat.cid from open_tb_itemcat as tb_itemcat
			  left join open_category as c on c.cid= tb_itemcat.cid where c.cid is null "; 
		$db->query($sql);
		$sql= "update open_category as c, open_tb_itemcat as tb_itemcat set tb_itemcat.cat_id =c.cat_id  where c.cid=tb_itemcat.cid" ;  
		$db->query($sql); 
		$sql= "update  open_category as c, (select ifnull(b.cat_id,0) as parent_cat_id,c.cat_id as cat_id from open_category as c
			   left join open_tb_itemcat as a on c.cid = a.cid  left join open_tb_itemcat as b on  a.parent_cid =b.cid ) as a
			   set  c.parent_id= a.parent_cat_id where a.cat_id=c.cat_id ";
		$db->query($sql); 
//	    echo ('itemcat2cat转换成功<br/>'); 
	}
	/**
	 *  宝贝2商品
	 * */
	function item2goods(){
		 global $_CFG, $db,$os,$topParamArr; 
		 $sql="select tb_item.num_iid, tb_item.cid,tb_ic.name as icname ,tb_item.title,tb_item.props,tb_item.property_alias,
				tb_item.seller_cids,tb_item.iid ,g.goods_name,g.goods_sn
				from open_tb_item as tb_item 
				left join open_tb_itemcat as  tb_ic on tb_item.cid =tb_ic.cid  
				left join open_goods as  g on tb_item.outer_id =g.goods_sn  where 1  and nick='".$_CFG['taobao_user']."' ".$where." ORDER BY  tb_item.id DESC";
		  $items_rows=$db->getAll($sql);
		  foreach ($items_rows as $items_row ){
			  	$iid = $items_row['iid'];
			  	$goods_sn= $items_row['goods_sn'];
			  	if( $iid && ($goods_sn=='' || !$goods_sn) ) TOP_item2OS_goods($iid);
		  } 
	}
	/**
	 *  品牌2品牌
	 * */
	function brand2brand(){
		global $_CFG, $db,$os,$topParamArr; 
		$sql="INSERT into open_brand(brand_name,brand_desc,sort_order,is_show,vid ) 
			select tb_brand.name,tb_brand.nick,1,1,tb_brand.vid from open_tb_brand  as tb_brand
			left join open_brand as b on b.vid=tb_brand.vid  
			where  b.vid is null and tb_brand.nick='".$_CFG['taobao_user']."'";
		$db->query($sql);
		$sql="update open_tb_brand as tb_brand, open_brand as b 
			 set tb_brand.brand_id = b.brand_id  
			 where tb_brand.vid =b.vid";
	    $db->query($sql); 
//		echo ('brand2brand<br/>'); 
	} 
	 /**
	 *  属性2颜色
	 * */
	function prop2color(){
		global $_CFG, $db,$os,$topParamArr; 
		$sql="insert into open_color(color_code,color_name,color_note,img_id,vid) 
			select distinct(tb_propvalue.vid), tb_propvalue.name,0,0,tb_propvalue.vid from open_tb_propvalue as tb_propvalue 
			left join open_color as s on s.vid = tb_propvalue.vid
			where tb_propvalue.prop_name='颜色' and s.vid is null";
		$db->query($sql);  
//		echo ('prop2color<br/>'); 
	}
	 /**
	 *  属性2尺码
	 * */
	function prop2size(){
		global $_CFG, $db,$os,$topParamArr; 
		$sql="insert into open_size(size_code,size_name,size_note,vid) 
			select distinct(tb_propvalue.vid), tb_propvalue.name,0,tb_propvalue.vid from open_tb_propvalue as tb_propvalue 
			left join open_size as s on s.vid = tb_propvalue.vid
			where ( tb_propvalue.prop_name like '%尺码%'  or tb_propvalue.prop_name like '%尺寸%' ) and s.vid is null"; 
		$db->query($sql);  
//		echo ('prop2size<br/>'); 
	}
	
	 /**
	 *  属性2系列
	 * */
	function prop2series(){
		global $_CFG, $db,$os,$topParamArr; 
		$sql="INSERT into open_series(series_code,series_name,default_item,vid ) 
			select tb_propvalue.vid, tb_propvalue.name,0,tb_propvalue.vid from open_tb_propvalue as tb_propvalue 
			left join open_series as s on s.vid = tb_propvalue.vid
			where tb_propvalue.prop_name like '%系列%' and s.vid is null ";
		$db->query($sql);  
//		echo ('prop2series<br/>'); 
	}
	
	 /**
	 *  属性2季节
	 * */
	function prop2season(){
		global $_CFG, $db,$os ; 
		$sql="insert into open_season(season_code,season_name,default_item,vid) 
			  select tb_propvalue.vid,  tb_propvalue.name,0,tb_propvalue.vid from open_tb_propvalue as tb_propvalue 
			  left join open_season as s on s.vid = tb_propvalue.vid
			  where tb_propvalue.prop_name like '%季节%' and s.vid is null  ";
		$db->query($sql);  
	}
     /**
	 *  交易2订单
	 * */
	function trade2order(){
			global $_CFG, $db,$os ;  
		    $item_list = get_tb_settings(null);   //取自动配置参数
		    //解析数据字段，以供展示用
		    $str_value = get_code_value($item_list,'auto_deal_time');
		    $str_arr = explode(';',$str_value); 
		    $recent_type = $str_arr[0];   //时间类型
		    if($recent_type == 1){ 
		    	$recent_time_select=$str_arr[1];
		    	if($recent_time_select == 1){
		    		$last_1  = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_1);
		    	}elseif ($recent_time_select == 2){
		    		$last_2  = mktime(0, 0, 0, date("m")  , date("d")-2, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_2);
		    	}elseif ($recent_time_select == 3){
		    		$last_3  = mktime(0, 0, 0, date("m")  , date("d")-3, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_3);
		    	}elseif ($recent_time_select == 7){
		    		$last_7  = mktime(0, 0, 0, date("m")  , date("d")-7, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_7);
		    	}elseif ($recent_time_select == 30){
		    		$last_30  = mktime(0, 0, 0, date("m")  , date("d")-30, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_30);
		    	}
		    }elseif ($recent_type == 2){
		    	$str_temp = explode('||',$str_arr[1]); 
		    	$start_date = $str_temp[0]; 
		    	$start_time = $start_date ;  
		    	$end_date = $str_temp[1]; 
		    	$end_time = $end_date ; 
		    }  
		    $lock_tag = get_code_value($item_list,'lock_tag'); //下载挂起标志位 如果为空则全都下载 
		    $download_tag = get_code_value($item_list,'download_tag');  //下载标志位 
		    $status = get_code_value($item_list,'status');
		    $status_keys = explode(';',$status);  
		    foreach ($status_keys as $stats)  //只下载选中了的状态标志位
		    {
		    	if(!$stats) continue;
				$userParamArr = array(
							'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders,seller_memo',
							'page_size' =>40,
					     	'page_no' => ((isset($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1)
				 	 );  
				if($stats!="ALL" &&  $stats!="") $userParamArr['status']=$stats;   
				if( $start_time  ) $userParamArr['start_created']=  $start_time;   
				if(  $end_time  ){
					$userParamArr['end_created'] =  $end_time ;  
				}else {
					$userParamArr['end_created'] =  date('Y-m-d H:i:s') ; 
				} 
		     	$result = TOP_TradesSoldGet($userParamArr);   
			  	if( $result['recordCounts'] > 40 ){ //多于一页的下载
				 	for ($i=0;$i<=$result['recordCounts']/40;$i++ ){ 
						$page=$i+1;   
					    $userParamArr = array(
										'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders,seller_memo',
										'page_size' =>40 , 
								     	'page_no' => $page
				     				);  	
						if($stats!="ALL" &&  $stats!="") $userParamArr['status']=$stats;   
						if( $start_time  ) $userParamArr['start_created']=$start_time;   
						if( $end_time  ) $userParamArr['end_created']=$end_time;     
						$result1 =TOP_TradesSoldGet($userParamArr); 
						foreach ( $result1['list'] as $row  ){   
							if( Check_is_exist('order_info','order_sn',Check_is_exist('tb_trade','tid',$row[tid],'','outer_order_sn'),'','order_id')) continue;
							 TradeToOrder($row[tid]);   
						}
					} 
				} elseif ( $result[recordCounts]>0 && $result[recordCounts]<=40 ){ //不满一页的下载
					   foreach ( $result['list'] as $row  ){ 
						    	if( Check_is_exist('order_info','order_sn',Check_is_exist('tb_trade','tid',$row[tid],'','outer_order_sn'),'','order_id')) continue;
							    TradeToOrder($row[tid]);   
					   		// if( Check_is_exist('order_info','order_sn',Check_is_exist('tb_trade','tid',$row[tid],'','outer_order_sn'),'','order_id')) continue;
							// if( !Check_is_exist('tb_trade','tid',$row[tid],'','outer_order_sn')   ){ //判断订单号线在系统中是否存在  不存在下载   
						   	//	 TradeToOrder($row[tid]); 
						    // 	}
						}
				}
		    } 
	}
	 /**
	 *  买家2会员
	 * */
	function buyer2user(){ 
		 global $_CFG, $db,$os,$topParamArr; 
		    $array1= $topParamArr;
		    $array1['method']='taobao.trades.sold.get';  
		    //API用户参数
			$userParamArr = array(
					'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders',
					'page_size' =>20,
			     	'page_no' => ((isset($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1)
		     	);
			  $result =  lib_getArray_params_page($array1 + $userParamArr , 'trade') ; 
			  if( $result['recordCounts'] > count($result['list']) ) // 总数量 $result['list'] pagesize=20
				{
					for ($i=0;$i<=$result['recordCounts']/count($result['list']);$i++ )
					{
						$page=$i+1;
					    $userParamArr = array(
										'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders',
										'page_size' =>20 ,
								     	'page_no' => $page
				     				);  
						$result1 =lib_getArray_params_page($array1 + $userParamArr,'trade');  //下载单页的goods	  
						foreach ( $result1['list'] as $row  ){  
							 GetInsertUserById($row['buyer_nick']); 
						}  
					}
				} elseif ( $result[recordCounts]>0 && $result[recordCounts]<20 ){ //不满一页的下载 
						foreach ( $result['list'] as $row  ){    
							 GetInsertUserById($row['buyer_nick']); 
						}  
				}
		 echo ('buyer2user<br/>'); 
	} 
	 /**
	 *  退款2退单
	 * */
	function Refund2order_return(){
		   $item_list = get_tb_settings(null);   //取自动配置参数
		    //解析数据字段，以供展示用
		    $str_value = get_code_value($item_list,'r_auto_deal_time');
		    $str_arr = explode(';',$str_value); 
		    $recent_type = $str_arr[0];   //时间类型
		    if($recent_type == 1){ 
		    	$recent_time_select=$str_arr[1];
		    	if($recent_time_select == 1){
		    		$last_1  = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_1);
		    	}elseif ($recent_time_select == 2){
		    		$last_2  = mktime(0, 0, 0, date("m")  , date("d")-2, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_2);
		    	}elseif ($recent_time_select == 3){
		    		$last_3  = mktime(0, 0, 0, date("m")  , date("d")-3, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_3);
		    	}elseif ($recent_time_select == 7){
		    		$last_7  = mktime(0, 0, 0, date("m")  , date("d")-7, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_7);
		    	}elseif ($recent_time_select == 30){
		    		$last_30  = mktime(0, 0, 0, date("m")  , date("d")-30, date("Y"));
		    		$start_time = date("Y-m-d H:m:s",$last_30);
		    	}
		    }elseif ($recent_type == 2){
		    	$str_temp = explode('||',$str_arr[1]); 
		    	$start_date = $str_temp[0]; 
		    	$start_time = $start_date ;  
		    	$end_date = $str_temp[1]; 
		    	$end_time = $end_date ; 
		    }   
		    $Refund_tag = get_code_value($item_list,'Refund_tag');  //下载退单标志位 
		    $status = get_code_value($item_list,'r_status');
		    $status_keys = explode(';',$status);  
   			foreach ($status_keys as $stats){  //只下载选中了的状态标志位 
		    			if(!$stats) continue;
		    			 $userParamArr = array(
								'fields' => "refund_id, tid, title, buyer_nick, seller_nick, total_fee, status, created, refund_fee, oid, good_status, company_name, sid, payment, reason, desc, has_good_return, modified, order_status ",
								'status'  => $stats , // 'WAIT_SELLER_AGREE',//WAIT_SELLER_AGREE(买家已经申请退款，等待卖家同意) WAIT_BUYER_RETURN_GOODS(卖家已经同意退款，等待买家退货)WAIT_SELLER_CONFIRM_GOODS(买家已经退货，等待卖家确认收货) SELLER_REFUSE_BUYER(卖家拒绝退款) 
											//CLOSED(退款关闭) SUCCESS(退款成功)  
								'type'     => 'guarantee_trade',
								'page_no'  => 1,
								'page_size' =>40,
							);  
					    $result = TOP_RefundsReceiveGet($userParamArr);  
					    if( $result['recordCounts'] > count($result['list']) ){ //多于一页的下载
							 	for ($i=0;$i<=$result['recordCounts']/40;$i++ )
								{
									$page=$i+1;
								    $userParamArr = array(
													'fields' => "refund_id, tid, title, buyer_nick, seller_nick, total_fee, status, created, refund_fee, oid, good_status, company_name, sid, payment, reason, desc, has_good_return, modified, order_status ",
													'status'  => $stats,  // 'WAIT_BUYER_RETURN_GOODS',//WAIT_SELLER_AGREE(买家已经申请退款，等待卖家同意) WAIT_BUYER_RETURN_GOODS(卖家已经同意退款，等待买家退货)WAIT_SELLER_CONFIRM_GOODS(买家已经退货，等待卖家确认收货) SELLER_REFUSE_BUYER(卖家拒绝退款) 
																//CLOSED(退款关闭) SUCCESS(退款成功)  
													'type'     => 'guarantee_trade',
													'page_no'  => 1,
													'page_size' =>40,
							     				);     
									$result1 = TOP_RefundsReceiveGet($userParamArr); 
									foreach ( $result1['list'] as $row  ){   
										if( !Check_is_exist('tb_refund','refund_id',$row[refund_id],'')    ){ //判断订单号线在系统中是否存在  不存在下载   
									   		 GetArrRefundGet($row[refund_id]); 
									   	}
									}
								}
						} elseif ( $result[recordCounts]>0 && $result[recordCounts]<= count($result['list']) ){ //不满一页的下载
								foreach ( $result['list'] as $row  ){ 
								     if( !Check_is_exist('tb_refund','refund_id',$row[refund_id],'')    ){ //判断订单号线在系统中是否存在  不存在下载   
										 	GetArrRefundGet($row[refund_id]);
									   	}
								}
						}  //end if 
		    } //end foreach
	}  
	/*****************taobao数据转换函数放入********涵数名入数据表open_transport中act字段*********************/
	/**
	 * 下载用户及订单(这里不转验证商品)
	 * */
	function InsertIntoTB_user_trade(){ 
			$userParamArr = array(
					'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders',
					'page_size' =>20 ,
			     	'page_no' => ((isset($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1)
		     	);   
			  $result = TOP_TradesSoldGet($userParamArr ); 
			  if( $result['recordCounts'] > count($result['list']) ) // 总数量 $result['list'] pagesize=20
				{
					for ($i=0;$i<=$result['recordCounts']/count($result['list']);$i++ )
					{  //页码($i+1)
						$page=$i+1; 
					    $userParamArr = array(
										'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders',
										'page_size' =>20 ,
								     	'page_no' => $page
				     				);   
						$result1 =TOP_TradesSoldGet($userParamArr ); 
						foreach ( $result1['list'] as $row  ){	//判断订单号线在系统中是否存在  不存在下载 
							if(   !Check_is_exist('tb_trade','tid',$row[tid],'','outer_order_sn') ) { //判断订单号线在系统中是否存在  不存在下载   
							   		if( $row[tid] && !Check_is_exist('tb_trade','tid',$row[tid])  )    DownTradeByTid($tid); 
									$result =GetArrTradeGet($row[tid]) ;  //高性能发交易状态并比较 本地交易状态发生变化则再次更新
									if( is_array($result['trade']) && $result['trade']['status'] != Check_is_exist('tb_trade','tid',$row[tid],'','status') )  DownTradeByTid($row[tid]); //状态发生变化则再次更新  
							        if(is_array($result['trade']) && $result['trade']['buyer_nick'] )  if( $result['trade']['buyer_nick']  && !Check_is_exist('tb_user','nick',$result['trade']['buyer_nick']) ) GetInsertUserById($result['trade']['buyer_nick']); //如果买家存在则转会员//更新送货时间，标致物 
							   	}   
						}  
					} 
				} elseif ( $result[recordCounts]>0 && $result[recordCounts]<20 ){ //不满一页的下载
					   foreach ( $result['list'] as $row  ){
							   	if(   !Check_is_exist('tb_trade','tid',$row[tid],'','outer_order_sn') ) { //判断订单号线在系统中是否存在  不存在下载   
							   		if( $row[tid] && !Check_is_exist('tb_trade','tid',$row[tid])  )    DownTradeByTid($tid); 
									$result =GetArrTradeGet($row[tid]) ;  //高性能发交易状态并比较 本地交易状态发生变化则再次更新
									if( is_array($result['trade']) && $result['trade']['status'] != Check_is_exist('tb_trade','tid',$row[tid],'','status') )  DownTradeByTid($row[tid]); //状态发生变化则再次更新  
							        if(is_array($result['trade']) && $result['trade']['buyer_nick'] )  if( $result['trade']['buyer_nick']  && !Check_is_exist('tb_user','nick',$result['trade']['buyer_nick']) ) GetInsertUserById($result['trade']['buyer_nick']); //如果买家存在则转会员//更新送货时间，标致物 
				 				}  
						} 	    
				} 
		
	}
    /**
     * TOP宝贵转成OS中的商品
     * ***/
    function TOP_item2OS_goods($iid){
    	global $_CFG, $exc,$db,$os,$topParamArr;  
    	if( !Check_is_exist("tb_item",'iid',$iid) )  GetTBOneItem( $iid );   
		itemcat2cat();
		brand2brand();
		prop2color();
		prop2size();
		prop2series();
		prop2season();
    	$sql="select * from open_tb_item as tb_item
			  left join open_goods as g on g.goods_sn= tb_item.outer_id
			  where iid='".$iid."'";  
    	$TOP_row = $db->getRow($sql);
    	if( !$TOP_row  ) return 0;
    	if( $TOP_row  && !$goods_id=$TOP_row['goods_id'] ){
    		  $max_id     =  $db->getOne("SELECT MAX(goods_id) + 1 FROM ".$os->table('goods'))  ;
    		  $goods_sn   = generate_goods_sn($max_id);/* 如果没有输入商品货号则自动生成一个商品货号 */ 
    		  $arr=array(
    		  			'goods_name' =>$TOP_row['title'],
						'goods_name_style' =>'1',
						'goods_sn' =>$goods_sn, 
						'cat_id' =>'0',
						'brand_id' =>'0',
						'shop_price' =>$TOP_row['price'],
						'market_price' =>$TOP_row['price'],
						'is_promote' =>'0', 
						'promote_price' =>$TOP_row['price'],
						'promote_start_date' =>0,
						'promote_end_date' =>0,
						'goods_img' => $TOP_row['pic_path'],
						'goods_thumb' => $TOP_row['pic_path'],
						'original_img' => $TOP_row['pic_path'],
						'keywords' =>'top下载',
						'goods_brief' =>'0',
						'seller_note' =>'0',
						'goods_weight' =>'0',
						'goods_number' =>$TOP_row['num'],  
						'warn_number' =>'0',
						'integral' =>'1',
						'give_integral' =>'-1',
						'is_best' =>'0',
						'is_new' =>'1',
						'is_hot' =>'0',
						'is_on_sale' =>'1', 
						'is_alone_sale' =>'1',
						'goods_desc' =>$TOP_row[item_desc],
						'add_time' =>gmtime(), 
						'last_update' =>gmtime(),
						'goods_type' =>'1',
						'rank_integral' =>'-1', 
						'size_lib_id' =>'0', 
						'storage_location' =>'1',
						'season_id' =>'0',
						'series_id' =>'0',
    		  		);
    		OS_InsertIntoTB('goods','goods_sn',$arr);
    		$goods_id =  Check_is_exist('goods','goods_sn',$goods_sn,'','goods_id'); 
    	} 
    	if(!Check_is_exist("tb_itemcat",'cid',$TOP_row['cid']) ) AddItemCat( $TOP_row['cid'] ) ;
		props2guige( $goods_id,$TOP_row['props'] ,$TOP_row['property_alias'],$TOP_row['cid'] ) ; //属性插入guige表中并 关联tb的guige1与guige2值
     	sku2inventory($goods_id,$TOP_row['iid'] ,$TOP_row['cid']  ) ;    //sku转成商品库存并关联　goods_color  goods_size  
	    PropImg2color_lib($goods_id,$TOP_row['iid'],$TOP_row['cid']); //taobao属性图片对应ＯＳ当中的色卡库 并关联open_goods_color 功能  
//	    itemcat2category($goods_id,$TOP_row['iid'] ,$TOP_row['cid']  ) ;  //把taobao宝贝类目转成本地分类的 
        Update_table_key("tb_item",'outer_id',$goods_sn,"iid='".$iid."'"); //关联tb_item当中outer_id
    	echo ('宝贝：['.$iid.'] 转换商品：<a href="../goods.php?id='.$goods_id.'" target="_blank">['.$goods_sn .']</a> !  <a href="#" onclick="history.go(-1)">返回</a><br/>'  );
    }
    /**
     * 得到均色代码
     * */
      function GetUnicolor(){ 
			$unicolor =	get_code_value(get_tb_settings(null) ,'unicolor') ;  
	   		if($unicolor) {
	   			return $unicolor;
	   		}else {
	   			return '000';
	   		} 
      }
    /**
     * 得到均码代码
     * */
      function GetUnisize(){ 
			$unisize = get_code_value(get_tb_settings(null),'unisize') ; 
			if($unisize) {
				return $unisize;
			}else {
				return '000';
			} 
      }  
	/**
	 * 维护均色均码
	 * */
	function keepguige(){
		if ( !$color_id= Check_is_exist('color','color_code',GetUnicolor(),'','color_id')  ){
			$arr =array(
					 'color_code' =>GetUnicolor(),
					 'color_name' =>'均色',
					 'color_note' =>'0',
					 'img_id'     =>'0'
					);
			OS_InsertIntoTB('color','color_id',$arr);
		}
		if ( !$size_id= Check_is_exist('size','size_code',GetUnisize(),'','size_id')  ){
			$arr = array(
					'size_code'   =>GetUnicolor(),
					'size_name'   =>'均码',
					'size_note'  =>'0'	
					);
			OS_InsertIntoTB('size','size_id',$arr);	
		}
	} 
	
	/**
	 * 根据配制自动下载交易
	 * */
	function auto_trade2order(){
	  	die('暂时不用了'); //暂时不用了
	}  
	
	/**
	 * 获得设置信息
	 *
	 * @param   array   $groups     需要获得的设置组
	 * @param   array   $excludes   不需要获得的设置组
	 *
	 * @return  array
	 */
	function get_tb_settings($code)
	{
	    global $db, $os; 
	    if($code){
	    	/* 取出全部数据：分组和变量 */
	    	$sql = "SELECT * FROM open_tb_config WHERE code = '".$code."'";
	    }else{
	    	$sql = "SELECT * FROM open_tb_config";
	    } 
	    $item_list = $db->getAll($sql); 
	    return $item_list;
	}
	
	function get_code_value($item_list,$code)
	{
		if( !is_array($item_list) ) return "";
		foreach ($item_list as $item)
		{
	    	if($item['code']==$code)
	    		return $item['value'];
	    }
	    return "";
	}
	
?>